package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.CustomerDao;
import com.example.demo.entity.Customer;
import com.example.demo.service.CustomerService;
import com.example.demo.utils.HttpRequestHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerDao customerDao;

    @Override
    public Integer apply(Customer customer,String openid) {//0:成功   1:电话号码已使用
        Integer rel=0;
        Customer customerIsExsit = customerDao.getCustomerByMobile(customer.getMobile());
        Customer customer1 = customerDao.findbyopenId(openid);

        if(customerIsExsit!=null){
            rel = 1;
        } else {
            if(customer1!=null){
                rel=2;
            } else {
                customer.setCreateTime(new Date());
                customer.setStatus(1);
                customer.setIsContract(0);
                customerDao.apply(customer);
            }
        }
        return rel;
    }

    @Override
    public Customer getCustomerByMobile(String mobile) {
        return customerDao.getCustomerByMobile(mobile);
    }

    @Override
    public Integer updateIsRaffled(Integer userId) {
        return customerDao.updateIsRaffled(userId);
    }

    @Override
    public String dengru(String code) {
        String url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxb87e0c7520781a8d&secret=10a8721944e3294fdf91ee22217b82bb&code="+code+"&grant_type=authorization_code";
        String data="{}";
        String s = HttpRequestHelper.HttpPost(url, data);
        System.out.println(s);
        JSONObject jsonObject = JSON.parseObject(s);
        String openid = (String) jsonObject.get("openid");
//        String openid =null;
//        try {
//            URL url = new URL( "/test1?code=001&name=测试");
//            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//            connection.setDoOutput(true); // 设置该连接是可以输出的
//            connection.setRequestMethod("GET"); // 设置请求方式
//            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//
//            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
//            String line = null;
//            StringBuilder result = new StringBuilder();
//            while ((line = br.readLine()) != null) { // 读取数据
//                result.append(line + "\n");
//            }
//            connection.disconnect();
//
//            String x = result.toString();
//            System.out.println(x);
//            openid=x;
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
       return openid;
   }

    @Override
    public Customer findbyopenId(String openId) {
        return customerDao.findbyopenId(openId);
    }

    @Override
    public List<Customer> getCustomerByDesId(String desId) {
        return customerDao.getCustomerByDesId(desId);
    }

    @Override
    public Integer count(String desId) {
        return customerDao.count(desId);
    }

    @Override
    public List<Customer> getCustomer(String keyword,String desId) {
        return customerDao.getCustomer(keyword,desId);
    }

    @Override
    public Customer getCustomerByuserId(Integer userId) {
        return customerDao.getCustomerByuserId(userId);
    }

    @Override
    public Integer updateIsContract(Integer userId) {
        return customerDao.updateIsContract(userId);
    }
}
